package com.project.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomBean;
import com.project.bean.SupplierBean;
import com.project.service.ISupplierService;
import com.project.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("supplier")
@Api(tags = "供应商")
public class SupplierController {
    @Autowired
    private ISupplierService supplierService;
    @ApiOperation(value = "分页动态查询供应商信息")
    @GetMapping("cutByItem")
    public IPage<SupplierBean> cutByItem(Integer pageNO, String name, String scope) {
        return supplierService.cutByItem(pageNO, name, scope);
    }
    @ApiOperation(value = "按id查询供应商信息")
    @GetMapping("findById")
    public SupplierBean findById(Integer id) {
        return  supplierService.findById(id);
    }
    @ApiOperation(value = "按id修改信息")
    @PutMapping("update")
    public String update(Integer id, String director, String phone) {
      supplierService.update(id, director, phone);
      return "ok";
    }
    @ApiOperation(value = "查看供应商是否存在")
    @GetMapping("findByName")
    public String findByName(String name) {
        if(supplierService.findByName(name)==null){
            return "no";
        }
        return "ok";
    }

    /**导入Excel供应商 */
    @PostMapping("uploadExcel")
    public ResponseEntity<String> upload(@RequestParam("excel") MultipartFile file) throws Exception {
        ImportParams params = new ImportParams();
        params.setSheetNum(1);
        params.setHeadRows(1);
        params.setTitleRows(1);
        /*
        * 如果发生异常，返回一个ResponseEntity带有HttpStatusof 的对象500 Internal Server Error和一条错误消息，指示出了什么问题。
        * 如果文件上传成功，该方法将返回一个ResponseEntity带有HttpStatusof的对象200 OK和一条指示文件上传成功的消息。*/
        try {
            // 读取数据并将其转换为供应商对象列表
            List<SupplierBean> list = ExcelImportUtil.importExcel(
                    file.getInputStream(), SupplierBean.class, params);
            // 循环添加
            for (SupplierBean supplierBean : list) {
                supplierService.add(supplierBean);
            }
            return ResponseEntity.ok("文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "添加供应商")
    @PostMapping("add")
    public String add(SupplierBean supplierBean) {
        supplierService.add(supplierBean);
        return "ok";
    }
    @ApiOperation(value = "查看所有供应商")
    @GetMapping("findAll")
    public List<SupplierBean> findAll() {
        return supplierService.findAll();
    }

}
